package com.slkj.shilixiaoyuanapp.ui.main.message;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.BaseLazyFragment;
import com.slkj.shilixiaoyuanapp.entity.MessageListEntity;
import com.slkj.shilixiaoyuanapp.entity.NoticeNumEntity;
import com.slkj.shilixiaoyuanapp.net.CommonResult;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.BaseFunctionCallBack;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.ObservableSource;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

/**
 * Created  on 2018-07-18
 *
 * @author 唐开阔
 * @describe 消息-通知
 */
public class MessageNoticeFragment extends BaseLazyFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    MessageNoticeAdapter adapter;
    List<MessageListEntity> messageNoticeEntityList = new ArrayList<>();
    @BindView(R.id.refresh)
    MySwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @BindView(R.id.statelayout1)
    StateLayout stateLayout1;
    @BindView(R.id.tv_num_school)
    CustomStateText tvNumSchool;
    @BindView(R.id.tv_school_mes)
    TextView tvSchoolMes;
    @BindView(R.id.tv_num_bj)
    CustomStateText tvNumBj;
    @BindView(R.id.tv_bj_mes)
    TextView tvBjMes;
    private int page = 1;
    private int count = PAGE_COUNT;
    //头部消息实体
    NoticeNumEntity noticeNumEntity;

    @Override
    protected int getResId() {
        return R.layout.fragment_message_notice;
    }

    @Override
    protected void onRealLoaded() {
        swipeRefreshLayout.setOnRefreshListener(this);
        stateLayout.setOnReLoadListener(this::onRefresh);
        recycerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MessageNoticeAdapter(messageNoticeEntityList, this);
        adapter.setOnLoadMoreListener(this::loadMore, recycerView);
        recycerView.setAdapter(adapter);
        swipeRefreshLayout.setEnabled(false);
        onRefresh();
    }


    @OnClick(R.id.layout_notice_school)
    public void onLayoutNoticeSchoolClicked() {
        MessageNoticeListActivity__JumpCenter.builder(getContext()).setNoticeType(MessageNoticeListActivity.TYPE_SCHOOL).create().go();
    }

    @OnClick(R.id.layout_notice_class)
    public void onLayoutNoticeClassClicked() {
        MessageNoticeListActivity__JumpCenter.builder(getContext()).setNoticeType(MessageNoticeListActivity.TYPE_CLASS).create().go();
    }

    @Override
    public void onRefresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().mainPageService()
                .getNoticeNum()
                .flatMap(new BaseFunctionCallBack<NoticeNumEntity, List<MessageListEntity>>() {
                    @Override
                    public ObservableSource<CommonResult<List<MessageListEntity>>> back(NoticeNumEntity result) {
                        noticeNumEntity = result;
                        return HttpHeper.get().mainPageService()
                                .getNoticeList(page, count);
                    }
                })
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<MessageListEntity>>() {
                    @Override
                    public void onCallBackSuccess(List<MessageListEntity> data) {
                        setNoticeData();
                        if (data.isEmpty()) {
                            stateLayout1.showEmptyView();
                        }else {
                            stateLayout1.showContentView();

                        }
                        setData(true, data);
                        swipeRefreshLayout.setRefreshing(false);
                        swipeRefreshLayout.setEnabled(true);
                        stateLayout.showContentView();
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        swipeRefreshLayout.setRefreshing(false);
                        stateLayout.showErrorView();
                    }
                });

    }

    /**
     * 设置通知头部
     */
    private void setNoticeData() {
        tvBjMes.setText(noticeNumEntity.getClassBody());
        tvSchoolMes.setText(noticeNumEntity.getSchoolBody());
        if (noticeNumEntity.getSchoolNum() != 0){
            tvNumSchool.setText(noticeNumEntity.getSchoolNumStr());
            tvNumSchool.setVisibility(View.VISIBLE);
        }else {
            tvNumSchool.setVisibility(View.GONE);
        }
        if (noticeNumEntity.getClassNum() != 0){
            tvNumBj.setText(noticeNumEntity.getClassNumStr());
            tvNumBj.setVisibility(View.VISIBLE);
        }else {
            tvNumBj.setVisibility(View.GONE);
        }
    }


    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().mainPageService()
                .getNoticeList(page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<MessageListEntity>>() {
                    @Override
                    public void onCallBackSuccess(List<MessageListEntity> data) {
                        setData(false, data);
                    }
                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }

    private void setData(boolean isRefresh, List data) {
        final int size = data == null ? 0 : data.size();
        page++;
        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }

}
